package Smallprogram.servict.impl;

import Smallprogram.dao.impl.OrderbiaoDaoImpl;
import Smallprogram.entiy.Orderbiao;

import java.util.ArrayList;
import java.util.List;

public class OrderbiaoServictImpl implements Smallprogram.servict.OrderbiaoServlet {
    OrderbiaoDaoImpl dao = new OrderbiaoDaoImpl();

    @Override
    public List<Orderbiao> selectorderbiao() {
        List<Orderbiao> listOrderbiao = dao.selectorderbiao();
        List<Orderbiao.Goods> listgoods = dao.selecgoods();
        for (int i = 0; i < listOrderbiao.size(); i++) {
            int index = 0;
            double amount = 0.00;
            int snacksid = listOrderbiao.get(i).getId();
            for (int x = 0; x < listgoods.size(); x++) {
                if (snacksid == listgoods.get(x).getOrderbiaoid()) {
                    List<Orderbiao.Goods> lisen = listOrderbiao.get(i).getGoods();
                    index += listgoods.get(x).getNumber();
                    amount += listgoods.get(x).getAmount();
                    if (lisen == null) {
                        lisen = new ArrayList<>();
                    }
                    lisen.add(listgoods.get(x));
                    listOrderbiao.get(i).setGoods(lisen);
                }

            }
            listOrderbiao.get(i).setAmount(amount);
            listOrderbiao.get(i).setGoods_num(index);
        }
        return listOrderbiao;
    }

    @Override
    public int selectMAXOrder() {
        return dao.selectMAXOrder();
    }

    @Override
    public int insertDetailsdb(Orderbiao.Goods goods) {
        return dao.insertDetailsdb(goods);
    }

    @Override
    public int insertOrderbiao(Orderbiao Oeder) {
        return dao.insertOrderbiao(Oeder);
    }

    @Override
    public List<Orderbiao> selectorderbiaoid(int id) {
        List<Orderbiao> listOrderbiao = dao.selectorderbiaoid(id);
        List<Orderbiao.Goods> listgoods = dao.selecgoods();
        for (int i = 0; i < listOrderbiao.size(); i++) {
            int index = 0;
            double amount = 0.00;
            int snacksid = listOrderbiao.get(i).getId();
            for (int x = 0; x < listgoods.size(); x++) {
                if (snacksid == listgoods.get(x).getOrderbiaoid()) {
                    List<Orderbiao.Goods> lisen = listOrderbiao.get(i).getGoods();
                    index += listgoods.get(x).getNumber();
                    amount += listgoods.get(x).getAmount();
                    if (lisen == null) {
                        lisen = new ArrayList<>();
                    }
                    lisen.add(listgoods.get(x));
                    listOrderbiao.get(i).setGoods(lisen);
                }

            }
            listOrderbiao.get(i).setAmount(amount);
            listOrderbiao.get(i).setGoods_num(index);
        }
        return listOrderbiao;
    }
}
